package com.dawn.module.hospital.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.date.DateUtils;
import com.dawn.framework.common.util.date.LocalDateTimeUtils;
import com.dawn.framework.common.util.json.JsonUtils;
import com.dawn.module.hospital.dal.dataobject.medicalcopy.MedicalCopyDO;
import com.dawn.module.hospital.dal.dataobject.msgsenduser.MsgSendUserDO;
import com.dawn.module.hospital.enums.BHConstants;
import com.dawn.module.hospital.enums.ErrorCodeConstants;
import com.dawn.module.hospital.enums.MsgSendTypeEnum;
import com.dawn.module.hospital.service.msgsenduser.MsgSendUserService;
import com.dawn.module.mp.api.msgtemplate.MsgTemplateApi;
import com.dawn.module.mp.api.msgtemplate.dto.TemplateMsgSendDTO;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2024/8/7 15:49
 */
@Slf4j
@Service
public class TemplateMsgService extends BHService {

    @Resource
    private MsgTemplateApi msgTemplateApi;

    @Resource
    private MsgSendUserService msgSendUserService;


    public CommonResult sendMsgOutpatient(Map map) {
        log.info("*********** sendMsgOutpatient start ***********");
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {
            TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
            templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
            templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdOutpatient());
            templateMsgSendDTO.setOpenid(String.valueOf(map.get("userId")));
            templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/order/detail?orderNumber="+map.get("orderNumber"));

            String first = "";
            String remark = "";

            String keyword5 = "门诊缴费";
            if(map.get("items")!=null){
                List<Map> orderItems = (List<Map>) map.get("items");
                if(orderItems!=null && orderItems.size()>0){
                    for (Map orderItem : orderItems) {
                        String prescriptionName = String.valueOf(orderItem.get("prescriptionName"));
                        if(StringUtils.isNotEmpty(prescriptionName) && !"null".equals(prescriptionName)){
                            keyword5 = keyword5+"|"+prescriptionName;
                            break;
                        }
                    }
                }
            }

            String keyword4 = String.valueOf(map.get("payModeName"));
            if(keyword4.indexOf("才登贝") != -1){
                keyword4 = "微信支付";
            }

            List<WxMpTemplateData> data = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dates = sdf.format(new Date());
            data.add(new WxMpTemplateData("first",first));
            data.add(new WxMpTemplateData("keyword1", String.valueOf(map.get("patientName"))));
            data.add(new WxMpTemplateData("keyword2", String.valueOf(map.get("orderNumber"))));
            data.add(new WxMpTemplateData("keyword3", "¥ "+String.valueOf(map.get("fee"))));
            data.add(new WxMpTemplateData("keyword4", keyword4));
            data.add(new WxMpTemplateData("keyword5", keyword5));
            data.add(new WxMpTemplateData("remark", remark));
            templateMsgSendDTO.setData(data);
            CommonResult result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            log.info("*********** sendMsgOutpatient ***********result:"+ JsonUtils.toJsonString(result));
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    public CommonResult sendMsgRegister(Map map) {
        log.info("*********** sendMsgRegister start ***********");
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {
            TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
            templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
            templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdRegister());
            templateMsgSendDTO.setOpenid(String.valueOf(map.get("userId")));
            templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/order/detail?orderNumber="+map.get("registerNo"));

            String first = "";
            String remark = "";

            String keyword4 = "-";
            if(map.get("doctorName")!=null && StringUtils.isNotBlank(String.valueOf(map.get("doctorName")))){
                keyword4 = String.valueOf(map.get("doctorName"));
            }

            String keyword5 = String.valueOf(map.get("regDate"));
            if(map.get("scheduleTime")!=null && StringUtils.isNotBlank(String.valueOf(map.get("scheduleTime")))){
                keyword5 = keyword5 +" "+ String.valueOf(map.get("scheduleTime"));
            }

            List<WxMpTemplateData> data = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            data.add(new WxMpTemplateData("first",first));
            data.add(new WxMpTemplateData("keyword1", String.valueOf(map.get("patientName"))));
            data.add(new WxMpTemplateData("keyword2", "马龙区中医医院"));
            data.add(new WxMpTemplateData("keyword3", String.valueOf(map.get("departmentName"))));
            data.add(new WxMpTemplateData("keyword4", keyword4));
            data.add(new WxMpTemplateData("keyword5", keyword5));
            data.add(new WxMpTemplateData("remark", remark));
            templateMsgSendDTO.setData(data);
            CommonResult result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            log.info("*********** sendMsgRegister ***********result:"+ JsonUtils.toJsonString(result));
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    public CommonResult sendMsgRegisterCancel(Map map) {
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {

            String orderNumber = String.valueOf(map.get("registerNo"));
            String totalCost = String.valueOf(map.get("amount"));

            TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
            templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
            templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdRegisterCancel());
            templateMsgSendDTO.setOpenid(String.valueOf(map.get("userId")));
            templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/order/detail?orderNumber="+orderNumber);

            String keyword4 = "-";
            if(map.get("doctorName")!=null && StringUtils.isNotBlank(String.valueOf(map.get("doctorName")))){
                keyword4 += String.valueOf(map.get("doctorName"));
            }

            List<WxMpTemplateData> data = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dates = sdf.format(new Date());
            data.add(new WxMpTemplateData("thing1", String.valueOf(map.get("patientName"))));
            data.add(new WxMpTemplateData("thing3", String.valueOf(map.get("departmentName"))));
            data.add(new WxMpTemplateData("thing4", keyword4));
            data.add(new WxMpTemplateData("amount9", totalCost));
            data.add(new WxMpTemplateData("character_string5", dates));
            templateMsgSendDTO.setData(data);
            CommonResult result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    public CommonResult sendMsgInHospitalPre(Map map) {
        log.info("*********** sendMsgInHospitalPre start ***********");
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {
            TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
            templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
            templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdInHospital());
            templateMsgSendDTO.setOpenid(String.valueOf(map.get("userId")));
            templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/order/detail?orderNumber="+map.get("orderNumber"));

            String first = "";
            String remark = "";

            String keyword5 = "住院预交";
            if(map.get("items")!=null){
                List<Map> orderItems = (List<Map>) map.get("items");
                if(orderItems!=null && orderItems.size()>0){
                    keyword5 = keyword5+"|"+String.valueOf(orderItems.get(0).get("prescriptionName"));
                }
            }

            String keyword4 = String.valueOf(map.get("payModeName"));
            if("云南才登贝".equals(keyword4)){
                keyword4 = "微信支付";
            }

            List<WxMpTemplateData> data = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dates = sdf.format(new Date());
            data.add(new WxMpTemplateData("first",first));
            data.add(new WxMpTemplateData("keyword1", String.valueOf(map.get("patientName"))));
            data.add(new WxMpTemplateData("keyword2", String.valueOf(map.get("inPatientNo"))));
            data.add(new WxMpTemplateData("keyword3", "¥ "+String.valueOf(map.get("payAmount"))));
            data.add(new WxMpTemplateData("keyword4", keyword4));
            data.add(new WxMpTemplateData("keyword5", keyword5));
            data.add(new WxMpTemplateData("remark", remark));
            templateMsgSendDTO.setData(data);
            CommonResult result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            log.info("*********** sendMsgInHospitalPre ***********result:"+ JsonUtils.toJsonString(result));
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    public CommonResult sendMsgRefund(String orderType,Map map) {
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {

            String orderNumber = "";
            String totalCost = "";
            if(BHConstants.ORDER_TYPE_REG.equals(orderType)){
                orderNumber = String.valueOf(map.get("registerNo"));
                totalCost = String.valueOf(map.get("amount"));
            } else if (BHConstants.ORDER_TYPE_RECIPE.equals(orderType)) {
                orderNumber = String.valueOf(map.get("orderNumber"));
                totalCost = String.valueOf(map.get("fee"));
            }

            TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
            templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
            templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdRefund());
            templateMsgSendDTO.setOpenid(String.valueOf(map.get("userId")));
            templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/order/detail?orderNumber="+orderNumber);

            String first = "";
            String remark = "";

            List<WxMpTemplateData> data = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dates = sdf.format(new Date());
            data.add(new WxMpTemplateData("first",first));
            data.add(new WxMpTemplateData("keyword1", orderNumber));
            data.add(new WxMpTemplateData("keyword2", totalCost));
            data.add(new WxMpTemplateData("keyword3", dates));
            data.add(new WxMpTemplateData("keyword4", "48小时内，具体时间以银行通知为准"));
            data.add(new WxMpTemplateData("remark", remark));
            templateMsgSendDTO.setData(data);
            CommonResult result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    public CommonResult sendRefundExamine(Map map) {
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {

            List<MsgSendUserDO> msgSendUserDOList = msgSendUserService.getListByType(MsgSendTypeEnum.V_REFUND_EXAMINE.getCode());
            log.info("*********** 退款申请通知用户 ***********msgSendUserDOList:"+JsonUtils.toJsonString(msgSendUserDOList));
            if(msgSendUserDOList.size() == 0){
                return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"消息推送用户无数据");
            }

            CommonResult result = CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"消息推送失败");
            for (MsgSendUserDO msgSendUserDO : msgSendUserDOList) {
                TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
                templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
                templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdRefundApply());
                templateMsgSendDTO.setOpenid(msgSendUserDO.getOpenid());
                templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/order/examine?examineId="+map.get("id"));

                String first = "";
                String remark = "";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dates = sdf.format(new Date());

                List<WxMpTemplateData> data = new ArrayList<>();
                data.add(new WxMpTemplateData("first",first));
                data.add(new WxMpTemplateData("keyword1", String.valueOf(map.get("orderNo"))));
                data.add(new WxMpTemplateData("keyword2", String.valueOf(map.get("patientName"))));
                data.add(new WxMpTemplateData("keyword3", String.valueOf(map.get("totalCost"))));
                data.add(new WxMpTemplateData("keyword4", dates));
                data.add(new WxMpTemplateData("keyword5", "订单状态异常，请核实"));
                data.add(new WxMpTemplateData("remark", remark));
                templateMsgSendDTO.setData(data);
                result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            }
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }


    /**
     * 病历复印申请处理通知
     *
     * @param medicalCopy
     * @return
     */
    public CommonResult sendMedicalApply(MedicalCopyDO medicalCopy) {
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {

            List<MsgSendUserDO> msgSendUserDOList = msgSendUserService.getListByType(MsgSendTypeEnum.V_MEDICAL_EXAMINE.getCode());
            log.info("*********** 病案复印申请成功通知管理员 ***********msgSendUserDOList:"+JsonUtils.toJsonString(msgSendUserDOList));
            if(msgSendUserDOList.size() == 0){
                return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"消息推送用户无数据");
            }

            CommonResult result = CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"消息推送失败");
            for (MsgSendUserDO msgSendUserDO : msgSendUserDOList) {
                TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
                templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
                templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdMedicalApply());
                templateMsgSendDTO.setOpenid(msgSendUserDO.getOpenid());
                templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/medicalcopy/examine?itemId="+medicalCopy.getId());

                String first = "";
                String remark = "";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dates = sdf.format(new Date());

                List<WxMpTemplateData> data = new ArrayList<>();
                data.add(new WxMpTemplateData("first",first));
                data.add(new WxMpTemplateData("thing4", medicalCopy.getPatientName()));
                data.add(new WxMpTemplateData("character_string3", medicalCopy.getHospitalNo()));
                data.add(new WxMpTemplateData("const1", "后台审核"));
                data.add(new WxMpTemplateData("const2", "待审核"));
                data.add(new WxMpTemplateData("remark", remark));
                templateMsgSendDTO.setData(data);
                result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            }
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    /**
     * 病历复印申请成功提醒
     *
     * @param medicalCopy
     * @return
     */
    public CommonResult sendMedicalExamineSuccess(MedicalCopyDO medicalCopy) {
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {


            TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
            templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
            templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdMedicalExamineSuccess());
            templateMsgSendDTO.setOpenid(medicalCopy.getOpenid());
            templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/medicalcopy/detail?itemId="+medicalCopy.getId());


            String thing12 = medicalCopy.getApplicantName();
            if(!"本人".equals(medicalCopy.getRelation())){
                thing12 += "| 患者:"+medicalCopy.getPatientName();
            }
            List<WxMpTemplateData> data = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dates = sdf.format(new Date());

            data.add(new WxMpTemplateData("thing12", thing12));
            data.add(new WxMpTemplateData("character_string3", medicalCopy.getHospitalNo()));
            data.add(new WxMpTemplateData("thing6", medicalCopy.getLeaveHospitalDept()));
            data.add(new WxMpTemplateData("character_string7", medicalCopy.getApplyNo()));
            data.add(new WxMpTemplateData("amount8", medicalCopy.getTotalPrice().toString()));
            templateMsgSendDTO.setData(data);
            CommonResult result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    /**
     * 病历审核拒绝提醒
     *
     * @param medicalCopy
     * @return
     */
    public CommonResult sendMedicalExamineFails(MedicalCopyDO medicalCopy) {
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {


            TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
            templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
            templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdMedicalExamineFails());
            templateMsgSendDTO.setOpenid(medicalCopy.getOpenid());
            templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/medicalcopy/detail?itemId="+medicalCopy.getId());


            String thing12 = medicalCopy.getApplicantName();
            if(!"本人".equals(medicalCopy.getRelation())){
                thing12 += "| 患者:"+medicalCopy.getPatientName();
            }
            List<WxMpTemplateData> data = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dates = sdf.format(new Date());

            data.add(new WxMpTemplateData("thing1", medicalCopy.getPatientName()));
            data.add(new WxMpTemplateData("character_string2", medicalCopy.getHospitalNo()));
            data.add(new WxMpTemplateData("thing3", medicalCopy.getLeaveHospitalDept()));
            data.add(new WxMpTemplateData("time4", LocalDateTimeUtil.format(medicalCopy.getCreateTime(), DatePattern.NORM_DATETIME_FORMATTER)));
            data.add(new WxMpTemplateData("const5", "病历不符合复印要求"));
            templateMsgSendDTO.setData(data);
            CommonResult result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    /**
     * 订单完成通知
     *
     * @param medicalCopy
     * @return
     */
    public CommonResult sendMedicalPaySuccess(MedicalCopyDO medicalCopy) {
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {


            TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
            templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
            templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdMedicalPaySuccess());
            templateMsgSendDTO.setOpenid(medicalCopy.getOpenid());
            templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/medicalcopy/detail?itemId="+medicalCopy.getId());


            String thing12 = medicalCopy.getApplicantName();
            if(!"本人".equals(medicalCopy.getRelation())){
                thing12 += "| 患者:"+medicalCopy.getPatientName();
            }
            List<WxMpTemplateData> data = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dates = sdf.format(new Date());

            data.add(new WxMpTemplateData("thing5", medicalCopy.getPatientName()));
            data.add(new WxMpTemplateData("thing1", "病案复印"));
            data.add(new WxMpTemplateData("thing6", medicalCopy.getLeaveHospitalDept()));
            data.add(new WxMpTemplateData("character_string2", medicalCopy.getApplyNo()));
            data.add(new WxMpTemplateData("amount7", medicalCopy.getTotalPrice().toString()));
            data.add(new WxMpTemplateData("time6", LocalDateTimeUtil.format(medicalCopy.getPayTime(), DatePattern.NORM_DATETIME_FORMATTER)));
            templateMsgSendDTO.setData(data);
            CommonResult result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    /**
     * 订单完成通知(管理员)
     *
     * @param medicalCopy
     * @return
     */
    public CommonResult sendMedicalPayToAdmin(MedicalCopyDO medicalCopy) {
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {

            List<MsgSendUserDO> msgSendUserDOList = msgSendUserService.getListByType(MsgSendTypeEnum.V_MEDICAL_EXAMINE.getCode());
            log.info("*********** 订单完成通知通知管理员 ***********msgSendUserDOList:"+JsonUtils.toJsonString(msgSendUserDOList));
            if(msgSendUserDOList.size() == 0){
                return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"消息推送用户无数据");
            }

            CommonResult result = CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"消息推送失败");
            for (MsgSendUserDO msgSendUserDO : msgSendUserDOList) {
                TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
                templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
                templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdMedicalPaySuccess());
                templateMsgSendDTO.setOpenid(msgSendUserDO.getOpenid());
                templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/medicalcopy/examine?itemId="+medicalCopy.getId());

                String first = "";
                String remark = "";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dates = sdf.format(new Date());

                List<WxMpTemplateData> data = new ArrayList<>();
                data.add(new WxMpTemplateData("thing5", medicalCopy.getPatientName()));
                data.add(new WxMpTemplateData("thing1", "病案复印"));
                data.add(new WxMpTemplateData("thing6", medicalCopy.getLeaveHospitalDept()));
                data.add(new WxMpTemplateData("character_string2", medicalCopy.getApplyNo()));
                data.add(new WxMpTemplateData("amount7", medicalCopy.getTotalPrice().toString()));
                data.add(new WxMpTemplateData("time6", LocalDateTimeUtil.format(medicalCopy.getPayTime(), DatePattern.NORM_DATETIME_FORMATTER)));
                templateMsgSendDTO.setData(data);
                result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            }
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    /**
     * 病历复印快递发出提醒
     *
     * @param medicalCopy
     * @return
     */
    public CommonResult sendMedicalDelivery(MedicalCopyDO medicalCopy) {
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {


            TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
            templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
            templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdMedicalDelivery());
            templateMsgSendDTO.setOpenid(medicalCopy.getOpenid());
            templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/medicalcopy/detail?itemId="+medicalCopy.getId());


            List<WxMpTemplateData> data = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dates = sdf.format(new Date());

            data.add(new WxMpTemplateData("thing1", medicalCopy.getPatientName()));
            data.add(new WxMpTemplateData("character_string2", medicalCopy.getHospitalNo()));
            data.add(new WxMpTemplateData("thing4", medicalCopy.getDeliveryName()));
            data.add(new WxMpTemplateData("character_string3", medicalCopy.getDeliveryNo()));
            data.add(new WxMpTemplateData("character_string6", medicalCopy.getApplyNo()));
            templateMsgSendDTO.setData(data);
            CommonResult result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

    /**
     * 病案待取件通知
     *
     * @param medicalCopy
     * @return
     */
    public CommonResult sendMedicalTake(MedicalCopyDO medicalCopy) {
        if (getTemplateMsgCfg() != null
                && getTemplateMsgCfg().getAppId() != null) {


            TemplateMsgSendDTO templateMsgSendDTO = new TemplateMsgSendDTO();
            templateMsgSendDTO.setAppid(getTemplateMsgCfg().getAppId());
            templateMsgSendDTO.setTemplateId(getTemplateMsgCfg().getTemplateMsgIdMedicalTake());
            templateMsgSendDTO.setOpenid(medicalCopy.getOpenid());
            templateMsgSendDTO.setUrl(getTemplateMsgCfg().getRootUrl()+"hosp/medicalcopy/detail?itemId="+medicalCopy.getId());


            List<WxMpTemplateData> data = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dates = sdf.format(new Date());

            data.add(new WxMpTemplateData("thing1", medicalCopy.getPatientName()));
            data.add(new WxMpTemplateData("thing3", medicalCopy.getRecipientAddress()));
            data.add(new WxMpTemplateData("const2", "自提"));
            data.add(new WxMpTemplateData("character_string4", medicalCopy.getHospitalNo()));
            templateMsgSendDTO.setData(data);
            CommonResult result = msgTemplateApi.sendTemplateMsg(templateMsgSendDTO);
            return result;
        }else {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"模板消息配置异常");
        }

    }

}
